<?php
/**
 * Created by PhpStorm.
 * User: dt
 * Time: 16:33
 */

namespace  backend\services;

use Yii;
use backend\base\BaseService;
use backend\models\AdminModel;

class AdminService extends BaseService
{

    /**
     * 初始化服务
     * @param string $className
     * @return mixed
     */
    public static function getInstance($className = __CLASS__)
    {
        return parent::getInstance($className);
    }

    /**
     * 修改密码
     * @param $newPass
     * @param int $userId
     * @param string $phone
     * @param string $oldPass
     * @param int $type  1 验证手机号修改  2验证旧密码修改
     * @return bool|int
     */
    public static function editPassword($newPass, $userId = 0, $phone = "", $oldPass = "",$type = 1)
    {
        if ($type == 1) {
            if (!$newPass || !$phone) {
                return false;
            }
            $user = AdminModel::find()->where(['mobile' => $phone])->one();
            if (!$user) {
                return -1;
            }
            $user->password = Yii::$app->security->generatePasswordHash($newPass.$user->create_time);
            if($user->save()){
                return true;
            } else {
                return false;
            }
        } else {
            if (!$newPass || !$oldPass || !$userId) {
                return false;
            }
            $user = AdminModel::findOne($userId);
            if ($user->validatePassword($oldPass)) {
                //修改密码
                // echo $user->password;
                $user->password = Yii::$app->security->generatePasswordHash($newPass);
                // echo $user->password;die;
                if($user->save()){
                    return true;
                }else {
                    return false;
                }
            } else {
                return -1;
            }
        }
    }

    /*
     * 系统属性列表
     */
    public static function getList($data)
    {
        $list = AdminModel::getInstance()->getList($data);
        if(!empty($list)){
            foreach ($list as $k => $v) {
                //创建人
                $list[$k]['creator'] = AdminModel::find()->select(['username'])->where(['id' => $v['creator_id']])->one()['username'];
            }
        }
        return $list;
    }

    /*
     * 用户详情
     */
    public static function getOne($data)
    {
        $info = AdminModel::find()->where(['id' => $data['id']])->one();
        return $info;
    }

    /*
     * 用户新增
     */
    public static function userAdd($data)
    {
        //新增用户
        $user = AdminModel::find()->where(['mobile' => $data['mobile']])->one();
        if(empty($user)){
            $data['password'] = Yii::$app->security->generatePasswordHash($data['password']);
            $model = new AdminModel(['scenario' => 'add']);
            if ($model->load($data, '') && $model->validate()) {//验证成功
                if ($model->save()) {//保存成功
                    return parent::getData(1,'operate_success_add');
                }
            }
        }else{
            $model = new AdminModel(['scenario' => 'edit']);
            if ($model->load($data, '') && $model->validate()) {//验证成功
                $count = $model->updateAll($data,'mobile=:mobile',array(':mobile'=>$data['mobile']));
            }
            if($count> 0){
                return parent::getData(1,'operate_success_edit');
            }
        }
        $errorMsg = array_values($model->getErrors());//报错信息
        return parent::getData(2,$errorMsg[0]);//失败提示
    }

    /*
     * 删除用户
     */
    public static function delete($data)
    {
        $model = AdminModel::findOne(intval($data['id']));
        if($model){
            $model->status = 2;
            if($model->save()){
                return parent::getData(1,'operate_success_add');
            }
        }
        $errorMsg = array_values($model->getErrors());//报错信息
        return parent::getData(2,$errorMsg[0]);//失败提示
    }
}
